﻿using Microsoft.SharePoint.WebControls;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace MetricGR.Interpolation.Components.CONTROLTEMPLATES
{
    public partial class InterpolationFieldUX : UserControl, IFieldEditor
    {
        public bool DisplayAsNewSection
        {
            get
            {
                // When this property is false, the editor appears in the Additional Column Settings 
                // section of the New Site Column, Change Site Column, Create Column, and Change Column pages. 
                // If the property value is true, the editor appears in its own section on the page.
                bool displayAsNewSection = false;
                return displayAsNewSection;
            }
        }

        public void InitializeWithField(Microsoft.SharePoint.SPField field)
        {
            if (!Page.IsPostBack)
            {
                InterpolationFieldObject obj;

                if (field == null)
                {
                    obj = new InterpolationFieldObject();

                    // TODO: It is new column, so initialize as
                    // needed with data from the HTTP context or
                    // with default values.
                }
                // An existing column is being edited.
                else
                {
                    InterpolationField customTypedField = (InterpolationField)field;

                    string json = customTypedField.GetFieldAttribute(Common.FieldInterpolationProperty);
                    obj = new InterpolationFieldObject(json);

                    // TODO: An existing column is being edited,
                    // so initialize as needed with the current 
                    // property values in customTypedField along 
                    // data from the HTTP context as needed.
                }

                this.uxInterpolationText.Text = obj.InterpolationString;
            }
        }

        public void OnSaveChange(Microsoft.SharePoint.SPField field, bool isNewField)
        {
            InterpolationFieldObject obj = new InterpolationFieldObject();
            obj.InterpolationString = this.uxInterpolationText.Text;
            string json = obj.ToJSON();

            // Save the additional settings as a custom property on this field
            InterpolationField customTypedField = (InterpolationField)field;
            customTypedField.SetFieldAttribute(Common.FieldInterpolationProperty, json);

            try
            {
                // error sometimes thrown here especially for 
                // new fields based on this field type, doesn't 
                // have an effect field.
                customTypedField.Update();
            }
            catch { }

            // TODO: Validate the values of the child controls
            // of the editor control and either throw SPException
            // or write values to customTypedField.
        }
    }
}
